package experiment5.task;

import java.sql.*;

/**
 * @author:fyl
 * @date 2021/5/18 15:10
 * @Modified By:
 * @Modified Date:
 * @Description:
 */
public class jdbc {
    private static final String url = "jdbc:mysql://localhost:3306/chat_message?useSSL=false";
    private static final String user = "root";
    private static final String pwd = "123456";
    private static  Connection connection = null;
    private static final PreparedStatement statement = null;
    private static final ResultSet resultSet = null;


    public static void query(){
        try {

            connection = DriverManager.getConnection(url,user,pwd);
            String sql = "select * from chat_message";
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                int sendId = resultSet.getInt(2);
                String sendName = resultSet.getString(3);
                int receiveId = resultSet.getInt(4);
                String receiveName = resultSet.getString(5);
                String message = resultSet.getString(6);
                System.out.println(id + "-"+sendId+"-"+sendName+"-"+message);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
                statement.close();
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        query();
    }

}
